//
//  LCYChatMainView.m
//  IMByLinkUs
//
//  Created by lcy on 16/1/14.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "LCYChatMainView.h"

typedef NS_ENUM(NSUInteger, LCYClickedBtnType) {
    LCYClickedBtnTypeEmoticon,    // 表情
    LCYClickedBtnTypeAttachment,  // 附件
    LCYClickedBtnTypeAudio,       // 语音
};

@interface LCYChatMainView ()

// 是否处于语音状态
@property (nonatomic, assign) BOOL isClickedAudioBtn;

// 长按录音按钮
@property (weak, nonatomic) IBOutlet UIButton *longPressToRecordBtn;


@end

@implementation LCYChatMainView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self initSubViews];
    
    // 监听键盘的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    // 添加点击轻拍事件
    UITapGestureRecognizer *tapChatView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
    
    [self.LCYMainChatView addGestureRecognizer:tapChatView];
    
    // 添加长按事件
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(shouldCallDelegateToStartRecord:)];
    
    [self.longPressToRecordBtn addGestureRecognizer:longPress];
}

// 初始化内部控件
- (void)initSubViews
{
    [self shouldAddEmoticonView];
    
    [self shouldAddAttachmentView];
    
    [self bringSubviewToFront:self.LCYBottomInputView];
    self.longPressToRecordBtn.hidden = YES;
}


#pragma mark - 按钮点击事件

// 显示或隐藏添加表情视图
- (IBAction)shouldPresentOrHiddenEmoticonPickerView:(id)sender {
    
    self.LCYEmoticonPickerBtn.selected = !self.LCYEmoticonPickerBtn.selected;
    self.LCYAttachmentAddBtn.selected = NO;
    self.LCYVoiceBtn.selected = NO;
    
    if(self.LCYEmoticonVC.isClickedEmoticonBtn)
    {
        self.longPressToRecordBtn.hidden = YES;
        // 隐藏键盘
        [self.LCYInputTextView resignFirstResponder];
        // 抬升inputView
        [self inputViewShouldMakeUpTransform:LCYClickedBtnTypeEmoticon];
        
        [self shouldAddEmoticonView];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
            
        }];
        
        self.LCYEmoticonVC.isClickedEmoticonBtn = NO;
        _isClickedAudioBtn = NO;
    }else
    {
        
        [self.LCYInputTextView becomeFirstResponder];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
        }];
        
        self.LCYEmoticonVC.isClickedEmoticonBtn = YES;
    }
}

// 显示或隐藏添加附件视图
- (IBAction)shouldPresentOrHideAttachmentView:(id)sender {
    
    self.LCYAttachmentAddBtn.selected = !self.LCYAttachmentAddBtn.selected;
    self.LCYEmoticonPickerBtn.selected = NO;
    self.LCYVoiceBtn.selected = NO;
    
    if(self.LCYAttachmentVC.isClickedAttachmentAddBtn)
    {
        self.longPressToRecordBtn.hidden = YES;
        // 隐藏键盘
        [self.LCYInputTextView resignFirstResponder];
        // 抬升inputView
        [self inputViewShouldMakeUpTransform:LCYClickedBtnTypeAttachment];
        
        [self shouldAddAttachmentView];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
            
        }];
        
        self.LCYAttachmentVC.isClickedAttachmentAddBtn = NO;
        _isClickedAudioBtn = NO;
    }else
    {
        
        [self.LCYInputTextView becomeFirstResponder];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
        }];
        
        self.LCYAttachmentVC.isClickedAttachmentAddBtn = YES;
    }
}

// 语音
- (IBAction)clickedAudioBtn:(id)sender {
    
    _isClickedAudioBtn = !_isClickedAudioBtn;
    _LCYVoiceBtn.selected = !_LCYVoiceBtn.selected;
    _longPressToRecordBtn.hidden = NO;
    
    if(_isClickedAudioBtn) // 处于语音状态
    {
        self.LCYAttachmentAddBtn.selected = NO;
        self.LCYEmoticonPickerBtn.selected = NO;
        
        // 键盘退下
        [self.LCYInputTextView resignFirstResponder];
        
        [UIView animateWithDuration:0.5 animations:^{
            
            // 如果当前正在添加附件
            if(!self.LCYAttachmentVC.isClickedAttachmentAddBtn)
            {
                self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
            }
            
            if (!self.LCYEmoticonVC.isClickedEmoticonBtn) {
                self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
            }
            
            self.LCYBottomInputView.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
            self.LCYMainChatView.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
            
        }];
        
    }else // 弹出键盘
    {
        [self.LCYInputTextView becomeFirstResponder];
    }
}

#pragma mark - 私有方法

// 键盘弹起和回收事件
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    // 0.取出键盘动画的时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    // 1.取得键盘最后的frame
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    // 2.计算控制器的view需要平移的距离
    CGFloat transformY = keyboardFrame.origin.y - self.frame.size.height;
    
    // 3.执行动画
    [UIView animateWithDuration:duration animations:^{
        self.LCYBottomInputView.transform = CGAffineTransformMakeTranslation(0.0f, transformY);
        self.LCYMainChatView.transform = CGAffineTransformMakeTranslation(0.0f, transformY);
        
        if(transformY != 0)
        {
            self.LCYEmoticonVC.isClickedEmoticonBtn = YES;
            self.LCYAttachmentVC.isClickedAttachmentAddBtn = YES;
            self.longPressToRecordBtn.hidden = YES;
        }
    }];
}

// 隐藏键盘
- (void)shouldHideKeyboard
{
    [self.LCYInputTextView resignFirstResponder];
}

// 显示键盘
- (void)shouldShowKeyboard
{
    [self.LCYInputTextView becomeFirstResponder];
}

// 输入框上移
- (void)inputViewShouldMakeUpTransform:(LCYClickedBtnType)clickedType
{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.LCYBottomInputView.transform = CGAffineTransformMakeTranslation(0.0f, -150.0f);
        self.LCYMainChatView.transform = CGAffineTransformMakeTranslation(0.0f, -150.0f);
        
        switch (clickedType) {
            case LCYClickedBtnTypeAttachment:
            {
                self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
                self.LCYEmoticonVC.isClickedEmoticonBtn = YES;
            }
                
                break;
                
            case LCYClickedBtnTypeEmoticon:
            {
                self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
                self.LCYAttachmentVC.isClickedAttachmentAddBtn = YES;
            }
                break;
            default:
                break;
        }
    }];
}

// 添加表情视图
- (void)shouldAddEmoticonView
{
    [self.LCYEmoticonVC.view removeFromSuperview];
    self.LCYEmoticonVC = nil;
    
    self.LCYEmoticonVC = [[LCYEmoticonPickerViewController alloc] init];
    
    [self addSubview:self.LCYEmoticonVC.view];
    
    self.LCYEmoticonVC.isClickedEmoticonBtn = YES;
    self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
}

// 添加附件视图
- (void)shouldAddAttachmentView
{
    [self.LCYAttachmentVC.view removeFromSuperview];
    self.LCYAttachmentVC = nil;
    
    self.LCYAttachmentVC = [[LCYAttachmentAddViewController alloc] init];
    
    [self addSubview:self.LCYAttachmentVC.view];
    
    
    self.LCYAttachmentVC.isClickedAttachmentAddBtn = YES;
    self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
    
    [self bringSubviewToFront:self.LCYAttachmentVC.view];
}

// 移除所有
- (void)shouldRemoveAllSubViews
{
    [self.LCYEmoticonVC.view removeFromSuperview];
    self.LCYEmoticonVC = nil;
    
    [self.LCYAttachmentVC.view removeFromSuperview];
    self.LCYAttachmentVC = nil;
}

// 回收键盘和表情选择器以及添加附件
-(void)viewTapped:(UITapGestureRecognizer*)tap1
{
    // 初始化
    self.LCYEmoticonPickerBtn.selected = NO;
    self.LCYAttachmentAddBtn.selected = NO;
    self.LCYVoiceBtn.selected = NO;
    
    [self endEditing:YES];
    
    [UIView animateWithDuration:0.5 animations:^{
        
        self.LCYBottomInputView.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
        self.LCYMainChatView.transform = CGAffineTransformMakeTranslation(0.0f, 0.0f);
        
        
        self.LCYEmoticonVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
        self.LCYAttachmentVC.view.transform = CGAffineTransformMakeScale(1.0f, 0.00f);
        
    }];
    
    self.LCYEmoticonVC.isClickedEmoticonBtn = YES;
    self.LCYAttachmentVC.isClickedAttachmentAddBtn = YES;
}

// 通知代理开始录音
- (void)shouldCallDelegateToStartRecord:(UIGestureRecognizer *)gestureRecognizer
{
    // 长按开始
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        self.longPressToRecordBtn.selected = YES;
        if(self.delegate && [self.delegate respondsToSelector:@selector(shouldStartRecord)])
        {
            [self.delegate shouldStartRecord];
        }
    }
    
    // 长按结束
    if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        self.longPressToRecordBtn.selected = NO;
        if(self.delegate && [self.delegate respondsToSelector:@selector(shouldCompleteRecord)])
        {
            [self.delegate shouldCompleteRecord];
        }
    }
}


@end
